package com.lx.nba.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.lx.nba.entiy.Man;

@Repository
@Transactional
public class ManDao {

	
	@PersistenceContext
	private EntityManager entityManager ;
	
	
	public void saveMan( Man man ){
		
		entityManager.persist(man);
		
	}
	
	public List<Man> getAllMan(){
		
		
		return entityManager.createNamedQuery("AllMan").getResultList() ;
		
	}
	
	public Man getMan( int playerId ){
		
		List<Man> mans = entityManager.createNamedQuery("ManByPlayerId").setParameter("playerId", playerId).getResultList() ;
		
		if( mans.isEmpty() ){
			
			return null ;
			
		}
		
		return mans.get(0)  ;
		
	}
	
	public Man getManById( int id ){
		
		List<Man> mans = entityManager.createNamedQuery("ManById").setParameter("id", id).getResultList() ;
		
		if( mans.isEmpty() ){
			
			return null ;
			
		}
		return mans.get(0) ;
	}
	
}
